const puppeteer = require('puppeteer');
const request = require('request');
let fs = require('fs');
let http = require('http');
let mkdirp = require('mkdirp');         // 创建文件
let index = 0;
let dir = './images';
mkdirp(dir, function(err) {
    if(err){
        console.log(err);
    }
});


function sleep(delay) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            try {
                resolve(1)
            } catch (e) {
                reject(0)
            }
        }, delay)
    })
}

function downLoadImg(imgUrl) {
    let name = imgUrl.toString().substring(imgUrl.toString().lastIndexOf("/"));
    http.get(imgUrl, function(res){
        let imgData = "";
        let contentLength = parseInt(res.headers['content-length']);
        res.setEncoding("binary");
        res.on("data", function(chunk){
            imgData += chunk;
            let process = ((imgData.length)/contentLength) * 100;
            let percent = parseInt(((process).toFixed(0)));
        });

        res.on("end", function(){
            fs.writeFile(`./images/${name}`, imgData, "binary", function(err){
                if(err){
                    console.log("下载失败");
                }else{
                    index++;
                    console.log(`第${index}张图片下载完成`);
                }


            });
        });
    });

}




// let name = imgUrl.toString().substring(imgUrl.toString().lastIndexOf("/"));


const NeteaseImg = async () => {
    const getOnePageData = async (page, pageNumber) => {
        const url = `http://jandan.net/ooxx/page-${pageNumber}#comments`;
        await page.goto(url,{waitUntil: 'networkidle2'});    // 跳转到页面    并且页面加载完成
        await page.setViewport({        // 设置浏览器的宽高
            width: 1300,
            height: 5227,
        });
        // 等待两秒，加载图片
        await page.waitFor(2000);

        const result = await page.evaluate(() => {
            let data = [] ;
            let elements = document.querySelectorAll('.row .text');
            for(let i of elements){
                let imgPath = i.querySelector('img').src;
                data.push({imgPath});
                console.log(i);
            }
            return data

        });
        return result;
    };



    // 循环获取数据
    let imgList = [];
    const browser = await puppeteer.launch({timeout: 300000, headless: false, args: ['--no-sandbox']});
    const page = await browser.newPage();


    //
    // const nextpage = await page.evaluate(() => {
    //     let elements = document.querySelector(".next-comment-page");
    //     return elements
    // })
    // console.log(nextpage);
    //
    //
    // getOnePageData(page,1)
    let filelen = 0;
    for(let i = 1 ; i <= 10 ; i++){
        const item = await getOnePageData(page, i);
        console.log(`总共获取到数据${filelen+=item.length}条。`);
        imgList = imgList.concat(item);

    }


    for(let i of imgList){
        downLoadImg(i.imgPath)
    }





    browser.close();

};

NeteaseImg();